<template>
  <uni-icons
    class="asset-scan"
    type="scan"
    :size="size"
    :color="color"
    @click="toSacn"
  ></uni-icons>
</template>

<script setup>
import { scanCodeAsset } from '@/api';

const props = defineProps({
  size: {
    type: Number,
    default: 40,
  },
  color: {
    type: String,
    default: '#fff',
  },
});

const emit = defineEmits(['scansuccess']);
const info = reactive({});

const toSacn = () => {
  uni.scanCode({
    success: (res) => {
      console.log('res.result', res.result);
      scanCodeAsset({
        assetCode: res.result,
      })
        .then((res) => {
          if (res.code === '0') {
            if (res.data.length) {
              emit('scansuccess', res.data);
            } else {
              uni.showToast({
                title: '未获取到该设备信息',
                icon: 'none',
              });
            }
          } else {
            uni.showToast({
              title: '获取信息失败',
              icon: 'none',
            });
          }
        })
        .catch((err) => {
          uni.showToast({
            title: '扫码失败',
            icon: 'none',
          });
        });
    },
  });
};
</script>

<style lang="scss" scoped></style>
